<template>
  <div class="module-wrap">
    <div class="title-wrap config title-wrap_3">
      <span class="title">测点统计</span>
      <div class="option">
        <span>单位 : 数量</span>
      </div>
    </div>
    <div id="chart" ref="chartRef" class="flex items-center justify-center">
      暂无数据
    </div>
  </div>
</template>

<script lang="tsx" setup>
import { onMounted, ref } from 'vue';
import * as echarts from 'echarts';
import 'echarts/theme/macarons';
import { monitorPointStatistics } from '../api';

let chart: any;
const chartRef = ref();
const init = async () => {
  const res = await monitorPointStatistics();
  const xData = [];
  const data = [];

  for (const item of res.data) {
    xData.push(item.structureName);
    data.push(item.pointCount);
  }

  if (!data.length) return;

  if (chart) chart.clear();
  chart = echarts.init(chartRef.value);
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow',
      },
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      top: '10%',
      containLabel: true,
    },
    xAxis: [
      {
        type: 'category',
        data: xData,
        axisTick: {
          alignWithLabel: true,
        },
        axisLine: {
          lineStyle: {
            color: 'rgba(255,255,255,.6)',
          },
        },
      },
    ],
    yAxis: [
      {
        type: 'value',
        splitLine: {
          show: true,
          lineStyle: {
            type: 'dashed',
            color: 'rgba(255,255,255,.26)',
          },
        },
        axisLine: {
          lineStyle: {
            color: 'rgba(255,255,255,.6)',
          },
        },
      },
    ],
    series: [
      {
        type: 'bar',
        barWidth: 40,
        itemStyle: {
          color: '#35c4fa',
        },
        data: data,
      },
    ],
  };
  chart.setOption(option);
  addEventListener(
    'resize',
    () => {
      chart.resize();
    },
    false,
  );
};
onMounted(() => {
  init();
});
</script>
